<?php
// 应用公共文件
// ini_set("display_errors", "On");
// ini_set("error_reporting","E_ALL & ~E_NOTICE");
// error_reporting(E_ALL | E_STRICT);
// error_reporting(E_ERROR | E_PARSE );
if (!function_exists('attr_format')) {
    /**
     * 格式化属性
     * @param $arr
     * @return array
     */
    function


    attr_format($arr)
    {
        $data = [];
        $res = [];
        $count = count($arr);
        if ($count > 1) {
            for ($i = 0; $i < $count - 1; $i++) {
                if ($i == 0) $data = $arr[$i]['detail'];
                // 替代变量1
                $rep1 = [];
                foreach ($data as $v) {
                    foreach ($arr[$i + 1]['detail'] as $g) {
                        //替代变量2
                        $rep2 = ($i != 0 ? '' : $arr[$i]['value'] . '_$_') . $v . '-$-' . $arr[$i + 1]['value'] . '_$_' . $g;
                        $tmp[] = $rep2;
                        foreach (explode('-$-', $rep2) as $k => $h) {
                            //替代变量3
                            $rep3 = explode('_$_', $h);
                            //替代变量4
                            $rep4['detail'][$rep3[0]] = isset($rep3[1]) ? $rep3[1] : '';
                        }
                        if ($count == count($rep4['detail']))
                            $res[] = $rep4;
                    }
                }
                $data = isset($tmp) ? $tmp : [];
            }
        } else {
            $dataArr = [];
            foreach ($arr as $k => $v) {
                foreach ($v['detail'] as $kk => $vv) {
                    $dataArr[$kk] = $v['value'] . '_' . $vv;
                    $res[$kk]['detail'][$v['value']] = $vv;
                }
            }
            $data[] = implode('-', $dataArr);
        }

        return [$data, array_merge(array_unique($res,SORT_REGULAR))];
    }
}

if (!function_exists('get_empty_object')) {
    function get_empty_object()
    {
        return (object)[];
    }
}

// 根据字段返回一维数组的数据
if (!function_exists('array_search_keys')) {
    function array_search_keys($array, $keys)
    {
        if (is_string($keys)) {
            $keys = explode(',', $keys);
        }
        $index = array_fill_keys($keys, 0);

        $data = [];
        foreach ($array as $key => $value) {
            if (isset($index[$key])) {
                $data[$key] = $value;
            }
        }
        return $data;
    }
}

if (!function_exists('empty_hGet')) {
    function empty_hGet($data){
        // 非数组的情况，代表未缓存过，需要做缓存
        if (!is_array($data)) return true;

        // 不存在id，代表未缓存过，需要做缓存
        if (!isset($data['id'])) return true;

        // $data['id'] == ''代表未缓存过，需要做缓存
        if ($data['id'] == '') return true;

        // $data['id']=0 代表未找到，不需要做缓存
        // $data['id'] > 0 代表已经缓存过，不需要做缓存

        return false;
    }
}

if (!function_exists('success')) {
    function success($data, $msg="获取数据成功") {
        $result['code'] = 200;
        $result['msg'] = $msg;
        $result['data'] = $data;
        return json($result);
    }
}

if (!function_exists('error')) {
    function error($data, $msg, $code=400) {
        $result['code'] = $code;
        $result['msg'] = $msg;
        $result['data'] = $data;
        return json($result);
    }
}
